//
//  $Id: XMLTreeTests.m 2 2007-09-07 18:35:43Z samson.etienne $
//  TCUBridgedClasses
//
//  Created by Simon Stapleton on Wed Oct 30 2002.
//  Copyright (c) 2001 Simon Stapleton. All rights reserved.
//

#import "TCUTree.h"
#import "TCUXMLTree.h"
#import "TCUXMLNode.h"

#import "NSString+TCUAdditions.h"
#import "TCUXMLNode.h"

#import "XMLTreeTests.h"

@implementation XMLTreeTests

- (void) testXMLGeneration {
  id docNode = [TCUXMLNode documentNodeWithURL:[NSURL URLWithString:@"http://www.tufty.co.uk"] encoding:NSUTF8StringEncoding];
  id xmlNode = [TCUXMLNode processingInstructionNodeWithName:@"xml" attributes:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1.0", @"yes", @"UTF8", nil] forKeys:[NSArray arrayWithObjects:@"version", @"standalone", @"encoding", nil]]];
  id elementNode = [TCUXMLNode elementNodeWithName:@"element" attributes:nil];

  id tree = [TCUTree treeWithObject:docNode];
  [tree addChild:[TCUTree treeWithObject:xmlNode]];
  [tree addChild:[TCUTree treeWithObject:elementNode]];

  STAssertEqualObjects([NSString stringWithData:[tree xmlData] encoding:NSUTF8StringEncoding], @"<?xml standalone=\"yes\" version=\"1.0\" encoding=\"UTF8\"?><element/>", @"");
}

- (void) testXMLDocumentCreation {
  id document;

  STAssertNoThrow (document = [TCUXMLTree treeForStandaloneXMLDocumentWithURL:[NSURL URLWithString:@"http://www.tufty.co.uk/crap.xml"] encoding:NSUTF8StringEncoding version:1.0], @"");

  STAssertEqualObjects([NSString stringWithData:[document xmlData] encoding:NSUTF8StringEncoding], @"<?xml encoding=\"UTF-8\" version=\"1.0\" standalone=\"yes\"?>", @"");

  STAssertNoThrow (document = [TCUXMLTree treeForXMLDocumentWithURL:[NSURL URLWithString:@"http://www.tufty.co.uk/crap.xml"] encoding:NSUTF8StringEncoding version:1.0 dtdURL:[NSURL URLWithString:@"http://www.tufty.co.uk/crap.dtd"] dtdRoot:@"element" dtdPublicName:nil], @"" );

  STAssertEqualObjects([NSString stringWithData:[document xmlData] encoding:NSUTF8StringEncoding], @"<?xml encoding=\"UTF-8\" version=\"1.0\" standalone=\"no\"?><!DOCTYPE element SYSTEM \"http://www.tufty.co.uk/crap.dtd\">", @"");

  STAssertNoThrow (document = [TCUXMLTree treeForXMLDocumentWithURL:[NSURL URLWithString:@"http://www.tufty.co.uk/crap.xml"] encoding:NSUTF8StringEncoding version:1.0 dtdURL:[NSURL URLWithString:@"http://www.tufty.co.uk/crap.dtd"] dtdRoot:@"element" dtdPublicName:@"-//tufty//DTD//EN"], @"" );

  STAssertEqualObjects([NSString stringWithData:[document xmlData] encoding:NSUTF8StringEncoding], @"<?xml encoding=\"UTF-8\" version=\"1.0\" standalone=\"no\"?><!DOCTYPE element PUBLIC \"-//tufty//DTD//EN\" \"http://www.tufty.co.uk/crap.dtd\">", @"");
}

- (void) testAddingNodes {
  id docNode;
  id subNode;
  id eleNode;

  docNode = [TCUXMLTree treeForStandaloneXMLDocumentWithURL:[NSURL URLWithString:@"http://www.tufty.co.uk/crap.xml"] encoding:NSUTF8StringEncoding version:1.0];
  subNode = [TCUXMLTree xmlTreeWithNode:[TCUXMLNode elementNodeWithName:@"element" attributes:nil]];
  eleNode = [TCUXMLTree xmlTreeWithNode:[TCUXMLNode elementNodeWithName:@"element2" attributes:nil]];
  
  STAssertNoThrow([docNode addXMLChild:subNode], @"");
  STAssertNoThrow([subNode addXMLChild:eleNode], @"");

  NSLog(@"%@", [NSString stringWithData:[docNode xmlData] encoding:NSUTF8StringEncoding]);
  NSLog(@"%@", docNode);
  NSLog(@"%@", subNode);
  NSLog(@"%@", eleNode);
  
  STAssertEqualObjects([NSString stringWithData:[docNode xmlData] encoding:NSUTF8StringEncoding], @"<?xml encoding=\"UTF-8\" version=\"1.0\" standalone=\"yes\"?><element><element2/></element>", @"");
  STAssertNoThrow ([subNode addXMLChild:[TCUXMLTree xmlTreeWithNode:[TCUXMLNode documentNodeWithURL:[NSURL URLWithString:@"p"] encoding:NSUTF8StringEncoding]]], @"");
  STAssertEqualObjects([NSString stringWithData:[docNode xmlData] encoding:NSUTF8StringEncoding], @"<?xml encoding=\"UTF-8\" version=\"1.0\" standalone=\"yes\"?><element><element2/></element>", @"");
}

- (void) testFindingNodes {
  id xmlPath = [[NSBundle bundleForClass:[XMLTreeTests class]] pathForResource:@"test" ofType:@"xml"];
  id tree;
  id elements;

  STAssertTrue (xmlPath != nil, @"");
  STAssertNoThrow(tree = [TCUXMLTree xmlTreeWithURL:[NSURL fileURLWithPath:xmlPath] options:TCUXMLParserNoOptions], @"");
  STAssertNoThrow(elements = [tree childTreesReferencingXMLNodesOfType:TCUXMLNodeTypeElement], @"");
  STAssertTrue ([elements count] == 1, @"");
  STAssertEqualObjects ([[[elements lastObject] object] dataString], @"Array", @"");
  STAssertNoThrow (elements = [[elements lastObject] childTreesReferencingXMLNodesOfType:TCUXMLNodeTypeElement], @"");
  STAssertTrue ([elements count] == 4, @"");
  STAssertEqualObjects ([[[[[elements lastObject] childTreesReferencingXMLNodesOfType:TCUXMLNodeTypeText] lastObject] object] dataString], @"String4", @"");
}
@end
